export class SkillMgr {
    private skills: Skill[] = [];

    // 添加技能
    addSkill(skill: Skill) {
        this.skills.push(skill);
    }

    // 移除技能
    removeSkill(skillId: number) {
        this.skills = this.skills.filter(s => s.id !== skillId);
    }

    // 获取可用技能
    getAvailableSkills(): Skill[] {
        return this.skills.filter(skill => skill.canUse());
    }

    // 释放技能
    useSkill(skillId: number, target: Character) {
        const skill = this.skills.find(s => s.id === skillId);
        if (!skill) return;

        // 检查目标是否有效
        if (!this.isValidTarget(skill, target)) return;

        // 触发释放前事件
        skill.onBeforeUse(target);

        // 执行技能效果
        skill.applyEffect(target);

        // 触发释放后事件
        skill.onAfterUse(target);
    }

    // 目标有效性检查
    private isValidTarget(skill: Skill, target: Character): boolean {
        // 根据技能目标类型和角色状态判断
        return skill.targetType === TargetType.ENEMY 
            ? target.isEnemy()
            : !target.isEnemy();
    }
}

// 技能类型枚举
export enum SkillType {
    ACTIVE = 'active', // 主动技能
    PASSIVE = 'passive' // 被动技能
}

// Buff类
export class Buff {
    id: number;
    duration: number; // 持续时间
    effects: SkillEffect[] = [];
    remainingTime: number;

    constructor(id: number, duration: number) {
        this.id = id;
        this.duration = duration;
        this.remainingTime = duration;
    }

    // 更新Buff
    update(deltaTime: number): boolean {
        this.remainingTime -= deltaTime;
        return this.remainingTime > 0;
    }

    // 应用Buff效果
    apply(target: Character) {
        this.effects.forEach(effect => effect.apply(target));
    }
}

// 技能类
export class Skill {
    id: number;
    name: string;
    type: SkillType;
    targetType: TargetType;
    effects: SkillEffect[] = [];
    buffs: Buff[] = []; // 技能关联的Buff

    constructor(id: number, name: string, type: SkillType, targetType: TargetType) {
        this.id = id;
        this.name = name;
        this.type = type;
        this.targetType = targetType;
    }

    // 判断技能是否可用
    canUse(): boolean {
        // 检查冷却、消耗等
        return true;
    }

    // 释放前事件
    onBeforeUse(target: Character) {
        // 触发技能释放前的事件
    }

    // 释放后事件
    onAfterUse(target: Character) {
        // 触发技能释放后的事件
    }

    // 应用技能效果
    applyEffect(target: Character) {
        this.effects.forEach(effect => effect.apply(target));
    }
}

// 技能效果接口
export interface SkillEffect {
    apply(target: Character): void;
}

// 伤害效果实现
export class DamageEffect implements SkillEffect {
    private damage: number;
    
    constructor(damage: number) {
        this.damage = damage;
    }

    apply(target: Character) {
        // 应用伤害效果
        target.takeDamage(this.damage);
    }
}

// 治疗效果实现
export class HealEffect implements SkillEffect {
    private amount: number;
    
    constructor(amount: number) {
        this.amount = amount;
    }

    apply(target: Character) {
        // 应用治疗效果
        target.heal(this.amount);
    }
}

// 目标类型枚举
export enum TargetType {
    SELF = 'self',
    ALLY = 'ally',
    ENEMY = 'enemy',
    ALL = 'all'
}

// 角色接口
export interface Character {
    id: number;
    isEnemy(): boolean;
    takeDamage(amount: number): void;
    heal(amount: number): void;
    
    // Buff相关方法
    addBuff(buff: Buff): void;
    removeBuff(buffId: number): void;
    updateBuffs(deltaTime: number): void;
    getBuffs(): Buff[];
    // 其他角色属性和方法
}
